package cn.tedu.ivos.geofence.service.impl;

import cn.tedu.ivos.base.exception.ServiceException;
import cn.tedu.ivos.base.response.StatusCode;
import cn.tedu.ivos.geofence.mapper.GeofenceMapper;
import cn.tedu.ivos.geofence.pojo.dto.GeofenceQuery;
import cn.tedu.ivos.geofence.pojo.dto.GeofenceSaveParam;
import cn.tedu.ivos.geofence.pojo.entity.Geofence;
import cn.tedu.ivos.geofence.pojo.vo.GeofenceVO;
import cn.tedu.ivos.geofence.service.GeofenceService;
import cn.tedu.ivos.vehicle.mapper.VehicleMapper;
import cn.tedu.ivos.vehicle.pojo.dto.VehicleQuery;
import cn.tedu.ivos.vehicle.pojo.entity.Vehicle;
import cn.tedu.ivos.vehicle.pojo.vo.VehicleVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class GeofenceServiceImpl implements GeofenceService {
    @Autowired
    GeofenceMapper geofenceMapper;
    @Autowired
    VehicleMapper vehicleMapper;
    @Override
    public List<GeofenceVO> selectGeofence(GeofenceQuery geofenceQuery) {
      log.debug("查询电子围栏信息geofenceQuery={}",geofenceQuery);
      List<GeofenceVO> list =geofenceMapper.selectGeofence(geofenceQuery);

      for(int i = 0; i < list.size(); i++){
          GeofenceVO geofenceVO = list.get(i);
          VehicleQuery vehicleQuery = new VehicleQuery();
          vehicleQuery.setGeofenceId(geofenceVO.getId());
          List<VehicleVO> vehicleList = vehicleMapper.selectVehicle(vehicleQuery);
          geofenceVO.setTotalNum(vehicleList.size());
          int availableNum = 0;
          for(VehicleVO vehicleVO : vehicleList){
              if(vehicleVO.getStatus().equals("1")){
                  availableNum++;
              }
          }
          geofenceVO.setAvailableNum(availableNum);
          geofenceVO.setVehicleList(vehicleList);
      }
      return list;


    }

    @Override
    public void updateStatus(Long geofenceId, String status) {
        log.debug("修改电子围栏状态业务:geofenceId={},status={}",geofenceId,status);
        Geofence geofence = new Geofence();
        geofence.setId(geofenceId);
        geofence.setStatus(status);
        geofence.setUpdateTime(new Date());
        geofenceMapper.update(geofence);
    }

    @Override
    public void deleteGeofence(Long id) {
        log.debug("删除电子围栏业务:id={}",id);
        /* 删除时一定要根据业务设计方案:围栏上有关联的车辆数据 */
        //1.封装车辆的查询条件
        VehicleQuery vehicleQuery = new VehicleQuery();
        vehicleQuery.setGeofenceId(id);
        //2.调用车辆模块的查车方法
        List<VehicleVO> list = vehicleMapper.selectVehicle(vehicleQuery);
        //3.如果该围栏下有绑定的车辆,无法删除,抛出异常
        if(list != null && list.size() > 0){
           throw new ServiceException(StatusCode.VEHICLE_EXIST);
        }else{
            //4.如果没有绑定的车辆,可以删除
            geofenceMapper.deleteById(id);
        }

    }

    @Override
    public void saveGeofence(GeofenceSaveParam geofenceSaveParam) {
        log.debug("保存电子围栏业务:geofenceSaveParam={}",geofenceSaveParam);
        Geofence geofence = new Geofence();
        BeanUtils.copyProperties(geofenceSaveParam,geofence);
        geofence.setStatus("1");
        geofence.setCreateTime(new Date());
        geofenceMapper.insert(geofence);

    }


}
